package com.youlai.boot.modules.game.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.youlai.boot.common.result.PageResult;
import com.youlai.boot.common.result.Result;
import com.youlai.boot.modules.game.model.form.GameUserRecordForm;
import com.youlai.boot.modules.game.model.query.GameUserRecordQuery;
import com.youlai.boot.modules.game.model.vo.GameUserRecordVO;
import com.youlai.boot.modules.game.service.GameUserRecordService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 游戏用户记录控制器
 *
 * @author 有来技术团队
 */
@Tag(name = "05.游戏记录管理")
@RestController
@RequestMapping("/api/v1/game/records")
@RequiredArgsConstructor
public class GameUserRecordController {

    private final GameUserRecordService gameUserRecordService;

    @Operation(summary = "分页查询用户游戏记录")
    @GetMapping("/page")
    public PageResult<GameUserRecordVO> getUserGameRecords(@Validated GameUserRecordQuery queryParams) {
        Page<GameUserRecordVO> result = gameUserRecordService.getUserGameRecords(queryParams);
        return PageResult.success(result);
    }

    @Operation(summary = "获取游戏记录详情")
    @GetMapping("/{id}")
    public Result<GameUserRecordVO> getUserGameRecord(@PathVariable Long id) {
        GameUserRecordVO record = gameUserRecordService.getUserGameRecord(id);
        return Result.success(record);
    }

    @Operation(summary = "更新游戏记录")
    @PutMapping("/{id}")
    public Result<Boolean> updateUserGameRecord(
            @PathVariable Long id,
            @Validated @RequestBody GameUserRecordForm form) {
        boolean result = gameUserRecordService.updateUserGameRecord(id, form);
        return Result.success(result);
    }

    @Operation(summary = "添加或更新游戏记录")
    @PostMapping
    public Result<Long> saveUserGameRecord(@Validated @RequestBody GameUserRecordForm form) {
        Long id = gameUserRecordService.saveUserGameRecord(form);
        return Result.success(id);
    }

    @Operation(summary = "获取游戏类型分布")
    @GetMapping("/type-distribution")
    public Result<List<Map<String, Object>>> getGameTypeDistribution(
            @Parameter(description = "用户ID") @RequestParam(required = false) Long userId) {
        List<Map<String, Object>> data = gameUserRecordService.getGameTypeDistribution(userId);
        return Result.success(data);
    }

    @Operation(summary = "获取游戏进度列表")
    @GetMapping("/progress-list")
    public Result<List<Map<String, Object>>> getGameProgressList(
            @Parameter(description = "用户ID") @RequestParam(required = false) Long userId,
            @Parameter(description = "限制数量") @RequestParam(defaultValue = "5") Integer limit) {
        List<Map<String, Object>> data = gameUserRecordService.getGameProgressList(userId, limit);
        return Result.success(data);
    }

    @Operation(summary = "增加游戏时长")
    @PostMapping("/{id}/play-time")
    public Result<Boolean> incrementPlayTime(
            @PathVariable Long id,
            @Parameter(description = "增加的时长(秒)") @RequestParam Integer increment) {
        boolean result = gameUserRecordService.incrementPlayTime(id, increment);
        return Result.success(result);
    }
} 